<?php
/**
 * This file is part of the Wicked package.
 *
 * Copyright Aymeric Assier <aymeric.assier@gmail.com>
 *
 * For the full copyright and license information, please view the Licence.txt
 * file that was distributed with this source code.
 *
 * - - - - - - - - - - - - - - - -
 *
 * Usefull functions
 *
 * @author Aymeric Assier <aymeric.assier@gmail.com>
 * @date 2012-10-05
 * @version 1.0
 */

use wicked\App;
use wicked\core\Forge;
use wicked\tools\Inflector;

/**
 * @param $path
 */
function path($path)
{
    return Loader::path($path);
}

/**
 * Add css html line
 * @return string
 */
function css()
{
	$str = '';
    foreach(func_get_args() as $file)
        $str .= '<link rel="stylesheet" href="' . asset('css/'.trim($file, '/').'.css').'" />' . "\n";
    return $str;
}


/**
 * Add js html line
 * @return string
 */
function js()
{
	$str = '';
    foreach(func_get_args() as $file)
        $str .= '<script src="' . asset('js/'.trim($file, '/').'.js') . '"></script>'."\n";
    return $str;
}


/**
 * Get file path
 * @param $file
 * @return string
 */
function asset($file)
{
	return APP_URL . config('folder::public') . '/' . $file;
}


/**
 * Get complete url
 * @param $path
 * @return string
 */
function url($path)
{
	return APP_URL . ltrim($path, '/');
}


/**
 * Load a file from public/view
 * @param $path
 * @param $data
 */
function view($path, $data = [])
{
	Forge::view($path, $data);
}


/**
 * Load a file from public/view
 * @param $path
 * @param $data
 */
function module($path, $data = [])
{
    // extract
    list($module, $action) = explode('::', $path);

    // run module
    $results = Forge::module($module, $action, $data);

    // run block
    $path = Inflector::flatten($module) . '/' . Inflector::flatten($action);
    Forge::block($path, $results);
}


/**
 * Get config
 * @param $key
 * @return mixed
 */
function config($key)
{
    return App::config($key);
}


/**
 * Add debug line
 * @param $text
 */
function trace($text)
{
	App::log($text);
}


/**
 * Die var_dump
 */
function debug()
{
	die(call_user_func_array('var_dump', func_get_args()));
}


/**
 * JSON data
 */
function json($data)
{
    die(json_encode($data));
}